<?php
namespace Agent\Controller;
use Think\Controller;
class IndexController extends BaseController {
	//欢迎页
	public function welcome(){
		// dump(I());
		$g=session('agent');
        $db_name=$g['db'];
        $m = M('List',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		// $m = M('department',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		$param = I('param.','','trim');
		
		$start_time=$param['start_time'];
		$end_time=$param['end_time'];
		
		$goods_no=$param['goods_no'];
		$goods_name=$param['goods_name'];
		$come_from=$param['come_from'];
		$merchant_code=$param['merchant_code'];
		
		if(!empty($goods_no)){
			$where['goods_no']=array('like','%$goods_no%');
		}
		if(!empty($goods_name)){
			$where['goods_name']=array('like','%$goods_name%');
		}
		if(!empty($come_from)){
			$where['come_from']=$come_from;
		}
		if(!empty($merchant_code)){
			$where['left(goods_no,10)']=array('like','%merchant_code%');//sql截取左边10位即商家编码
		}
		
		if($start_time!='' && $end_time==''){
			$start_time=strtotime($start_time);
			$end_time=strtotime(date('Y-m-d',time()+86400));
        }else if($start_time=='' && $end_time!=''){
			$end_time=strtotime($end_time);
			$start_time=strtotime(date('Y-m-d',time()));
        }else if($start_time=='' && $end_time==''){
			$start_time=strtotime(date('Y-m-d',time()));
			$end_time=strtotime(date('Y-m-d',time()+86400));
		}else if($start_time!='' && $end_time!=''){
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time);
		}

		$map['UNIX_TIMESTAMP(created_at)']= array(array('gt',$start_time),array('lt',$end_time));
		
		$nums_month=getMonthNum(date('Y-m-d',$start_time),date('Y-m-d',$end_time));
		
		if($end_time-$start_time<=86400){//一天内
			//按小时统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),'%Y-%m-%d %H:00:00') t_time")->where($map)->group("FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),'%Y-%m-%d %H')")->select();//时间列表
		}
		
		if($end_time-$start_time>86400 and $nums_month<=1){//一个月内
			//按天统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),'%Y-%m-%d') t_time")->where($map)->group("FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),'%Y-%m-%d')")->select();//时间列表
		}
		
		if($nums_month>1 && $nums_month<=12){//一年内
			//按月统计
			$list=$m->field("count(1) t_count,FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),'%Y-%c') t_time")->where($map)->group("FROM_UNIXTIME(UNIX_TIMESTAMP(created_at),'%Y-%m')")->select();//时间列表
		}
		// echo $m->getlastsql();
		// die;
		$count_all=0;
			
		if($end_time-$start_time<=86400){//一天内
			$categories="";
			$datas="";
			$categories=',"00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00","08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00","24:00"';
			$cate_list=explode(',',substr($categories,1));
			foreach($cate_list as $ks=>$vs){
				$ii=0;
				foreach($list as $k=>$v){
					$ii++;
					// dump($ii);
					// dump(count($list));
					// dump($ii==count($list));
					// dump($v['t_time']);
					// dump(intval(date("H",strtotime($v['t_time']))));
					if($ks==intval(date("H",strtotime($v['t_time'])))){
						$datas.=','.$v['t_count'];
						$count_all+=$v['t_count'];
						break;
					}
					if($ii==count($list)){
						$datas.=',0';
					}
				}
			}
		}

		if($end_time-$start_time>86400 and $nums_month<=1){//一个月内
			$categories="";
			$datas="";
			$days=($end_time-$start_time)/86400+1;//相差天数
			// dump($days);
			for($i=0;$i<$days;$i++){
				$categories.=',"'.date('Y-m-d',$start_time).'"';
				$ii=0;
				foreach($list as $k=>$v){
					$ii++;
					// dump(date('Y-m-d',$start_time)==$v['t_time']);
					// dump(date('Y-m-d',$start_time));
					// dump($ii==count($list));
					if(date('Y-m-d',$start_time)==$v['t_time']){
						$datas.=','.$v['t_count'];
						$count_all+=$v['t_count'];
						break;
					}
					if($ii==count($list)){
						$datas.=',0';
					}
				}
				$start_time+=86400;
			}
		}
		
		if($nums_month>1 && $nums_month<=12){//一年内
			$categories="";
			$datas="";
			$t_month=intval(date('m',$start_time));//开始月份
			$t_year=intval(date('Y',$start_time));//开始月份
			$datename=date('Y-'.$t_month,$start_time);//开始日期
			$next_month=0;//下一年开始月份
			for($i=0;$i<$nums_month+1;$i++){
				$categories.=',"'.$datename.'"';
				$ii=0;
				foreach($list as $k=>$v){
					$ii++;
					if($datename==$v['t_time']){
						$datas.=','.$v['t_count'];
						$count_all+=$v['t_count'];
						break;
					}
					if($ii==count($list)){//如果找不到匹配时间（即查询结果中无结果，则返回值为0）
						$datas.=',0';
					}
				}
				if($t_month+$i<12){
					$datename=date('Y-'.($t_month+$i+1),$start_time);//更新下月时间
					
				}else{
					$next_month++;
					$datename=($t_year+1).'-'.$next_month;//更新下月时间
				}
			}
		}
		
		if($nums_month>12){//大于1年
			$this->error('查询时间不能大于一年！');
		}
		
		// dump($categories);
		// dump($datas);
		
		// dump($datas);
		// dump($categories);
		// echo $m->getlastsql();
		$pushorder_list=$m->limit(0,15)->select();
		$this->assign('pushorder_list',$pushorder_list);
		
		$this->assign('count_all',$count_all);
		$this->assign('categories',substr($categories,1));
		$this->assign('datas',substr($datas,1));
		$this->assign('list_t',$list);
		$this->display();
	}
	//首页
    public function index(){
        //////类目展示
        $g=session('agent');
		$gc=session('agent.childs');
		if(empty($gc)){//从用户
			$db_name=$g['db'];
			$department_id=$gc['department_id'];
			$m = M('department',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$book = M('Book',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
			$root_group_id=$m->where(array('id'=>$department_id))->getField('root_group_id');
			$group_id=explode(',',$root_group_id);
			if($g['type']==0){
				$map['id']=array('in',$group_id);//从用户判断角色权限
			}
		}
		$map['is_menu']=1;
		$root	=	M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)
					->where($map)
					->order("sort")->select();
		foreach($root as $kss=>$vss){
			$actions=explode(',',$vss['action']);
			if(count($actions)>1){
				$root[$kss]['action']=$actions[0];//设置菜单对应控制器为第一个字符串
			}
		}
        $new_arr=array();
		foreach($root as $k=>$v){
            if($v['pid']==0){//顶级菜单
                foreach($root as $j=>$u){
					if($v['id']==$u['pid']){
						foreach($root as $js=>$us){
							if($us['pid']==$u['id']){
								if(in_array($us['id'],$group_id) || $g['type']==1){
									$u['child'][]=$us;
								}
							}
						}
						$v['child'][]=$u;
					}
				}
				$new_arr[]=$v;
            }
        }
		
		$member_id=session('agent.member_id');
		$user_id=session('agent.id');
		$warehouse=M('Warehouse');
		$uw=M('UserWarehouse');
		// $warehouse_list=$warehouse->where(array('member_id'=>$member_id))->select();//录属于当前企业的仓库
		$uw_info=$uw->where(array('user_id'=>$user_id))->find();
		$warehouse_idlist=array_unique(explode(',',$uw_info['warehouse_group_id']));
		$warehouse_list=$warehouse->where(array('id'=>array('in',$warehouse_idlist)))->select();
		// dump(session());
		
		$this->assign('warehouse_list',$warehouse_list);
		// $manua_id_list=$book->where(array('examined'=>2))->select();
		if(empty($gc)){//从用户
			$manua_id_list=$book->select();
			foreach($manua_id_list as $ks=>$vs){
				$arr_list[]=$vs['book_no'];
			}
			$manua_idlist=implode(',',$arr_list);
			$this->assign('manua_idlist',$manua_idlist);
		}
        /////获取消息总数
        $massage_count=M('massage',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)->where("status=0")->count();
        $this->assign('massage_count',$massage_count);
        $this->assign('list',$new_arr);
		
        $this->display();
    }
}